package com.example.service;

import com.example.entity.LoginUser;
import com.example.mapper.LoginUserMapper;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class LoginUserService implements UserDetailsService {

    @Resource
    LoginUserMapper mapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        LoginUser user = mapper.loginAuthor(username);
        if(user==null) throw new UsernameNotFoundException("用户" + username + "登录失败，用户名不存在");

        return User
                .withUsername(user.getUsername())
                .password(user.getPassword())
                .roles(user.getRole())
                .build();
    }
}
